#include "HSectionNumericalCalculator.h"

#include "DataStructure/Common/Real.h"

#include "SectionCalculator/StandardSectionTool.h"

using SG::Algebra::Point;
using SG::DataStructure::Common::Real;
using SG::DataStructure::FEM::PBARLData;
using SG::FEMSolver::Element::HSectionNumericalCalculator;

void HSectionNumericalCalculator::Mesh (const PBARLData& section, _OUT Meshes& meshes, int refineLevel) const
{
    Real w1{ section.m_dimension[2] };
    Real w2{ section.m_dimension[0] + section.m_dimension[1] };
    Real t1{ 0.5 * section.m_dimension[1] };
    Real t2{ section.m_dimension[3] };

    int n{ refineLevel + 1 };
    int nt1{ n };
    int nt2{ n };
    int nw1{ n };
    int nw2{ 2 * n };

    StandardSectionTool secMesh;
    Point               point;
    int                 nodeId{ 0 };
    int                 eleId{ 0 };
    Meshes              newMeshes;
    NodeSet             newEdgeNodes;
    int                 numRepeatNodes{ 0 };
    NodeSet             edgeNodes;

    // 1st block
    point.m_y = 0.0;
    point.m_z = 0.0;
    secMesh.MeshRect2 (point, t1, 0.5 * (w1 - t2), nt1, nw1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 2nd block
    point.m_z = 0.5 * (w1 + t2);
    secMesh.MeshRect2 (point, t1, -t2, nt1, nt2, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 3rd block
    point.m_z = w1;
    secMesh.MeshRect2 (point, t1, -0.5 * (w1 - t2), nt1, nw1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 4th block
    point.m_y = w2 - t1;
    point.m_z = 0.5 * (w1 - t2);
    secMesh.MeshRect (point, -(w2 - 2 * t1), t2, nw2, nt2, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt2 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 5th block
    point.m_y = w2;
    secMesh.MeshRect (point, -t1, t2, nt1, nt2, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt2 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 6th block
    point.m_y = w2 - t1;
    point.m_z = 0.0;
    secMesh.MeshRect2 (point, t1, 0.5 * (w1 - t2), nt1, nw1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 7th block
    point.m_z = w1;
    secMesh.MeshRect2 (point, t1, -0.5 * (w1 - t2), nt1, nw1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
}